package action;

import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import dicts.GlobalDict;
import dicts.RealmDict;
import entities.TLink;
import entities.TSubject;
import entities.TWebsite;
import interfaces.ILinkService;
import interfaces.ISubjectService;
import interfaces.IWebsiteService;
import util.ParameterUtil;
import utils.AesUtil;
import utils.NumberUtil;
import utils.StringUtil;

public class LinkAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private ILinkService linkService;
	private IWebsiteService websiteService;
	private ISubjectService subjectService;

	private TLink linkBean;
	private List<TLink> linkList;

	private String lnkNo;
	private String lnkNos;
	private String lnkName;
	private String lnkInfo;
	private String lnkState;
	private Date lnkDate;
	private Date lnkDateTop;
	private Date lnkDateEnd;
	private String lnkRemark;

	private String txnOrder;
	private String txnCur;
	private String txnLoc;
	private String txnProduct;
	private String txnFname;
	private String txnLname;
	private String txnPhone;
	private String txnEmail;
	private String txnCountry;
	private String txnState;
	private String txnCity;
	private String txnAddr;
	private String txnZip;

	private TWebsite websiteBean;
	private List<TWebsite> websiteList;
	private String merNo;
	private String webNo;
	private String webNos;
	private String webSign;
	private String webAddress;
	private String webDescript;
	private String webOwner;
	private String webRegister;
	private Double webValidity;
	private String webState;
	private Date webDate;
	private Date webDateTop;
	private Date webDateEnd;
	private String webVstate;
	private Date webVdate;
	private Date webVdateTop;
	private Date webVdateEnd;
	private String webRemark;

	public String show() throws Exception {
		if (!linkService.exist(lnkNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			linkBean=linkService.fetch(lnkNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("webDefault", ActionContext.getContext().getSession().get("query").toString());
		paramMap.put("webState", "1");

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "webDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TWebsite> resultSet = websiteService.search(querySet);
		websiteList = resultSet.getResultList();
		if (websiteList.size() == 0) {
			addActionError("该账号名下没有可用的网站,请先为当前账户提交至少一个可用的网站.");
			return INFO;
		}
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		linkBean = new TLink();
		linkBean.setLnkNo(lnkNo);
		linkBean.setLnkName(lnkName);
		StringBuffer sb = new StringBuffer();
		sb.append(RealmDict.PORTAL + "Payment?");
		sb.append("version=" + GlobalDict.VERSION + "&");

		sb.append("no=" + URLEncoder.encode(AesUtil.AES_Encrypt(AesUtil.KEY, ((TSubject) ActionContext.getContext().getSession().get("subject")).getSubNo()), "UTF-8") + "&");
		sb.append("key=" + URLEncoder.encode(AesUtil.AES_Encrypt(AesUtil.KEY, ((TSubject) ActionContext.getContext().getSession().get("subject")).getSubKey()), "UTF-8") + "&");
		sb.append("order=" + txnOrder + "&");
		sb.append("site=" + URLEncoder.encode(lnkName, "UTF-8") + "&");
		sb.append("cur=" + txnCur + "&");
		sb.append("loc=" + NumberUtil.transfer(txnLoc) + "&");
		sb.append("notify=" + URLEncoder.encode("http://" + lnkName, "UTF-8") + "&");
		sb.append("product=" + URLEncoder.encode(txnProduct, "UTF-8") + "&");
		sb.append("cfname=" + URLEncoder.encode(txnFname, "UTF-8") + "&");
		sb.append("clname=" + URLEncoder.encode(txnLname, "UTF-8") + "&");
		sb.append("cphone=" + URLEncoder.encode(txnPhone, "UTF-8") + "&");
		sb.append("cemail=" + URLEncoder.encode(txnEmail, "UTF-8") + "&");
		sb.append("ccountry=" + URLEncoder.encode(txnCountry, "UTF-8") + "&");
		sb.append("cstate=" + URLEncoder.encode(txnState, "UTF-8") + "&");
		sb.append("ccity=" + URLEncoder.encode(txnCity, "UTF-8") + "&");
		sb.append("caddr=" + URLEncoder.encode(txnAddr, "UTF-8") + "&");
		sb.append("czip=" + URLEncoder.encode(txnZip, "UTF-8") + "&");
		sb.append("hfname=" + URLEncoder.encode(txnFname, "UTF-8") + "&");
		sb.append("hlname=" + URLEncoder.encode(txnLname, "UTF-8") + "&");
		sb.append("hphone=" + URLEncoder.encode(txnPhone, "UTF-8") + "&");
		sb.append("hemail=" + URLEncoder.encode(txnEmail, "UTF-8") + "&");
		sb.append("hcountry=" + URLEncoder.encode(txnCountry, "UTF-8") + "&");
		sb.append("hstate=" + URLEncoder.encode(txnState, "UTF-8") + "&");
		sb.append("hcity=" + URLEncoder.encode(txnCity, "UTF-8") + "&");
		sb.append("haddr=" + URLEncoder.encode(txnAddr, "UTF-8") + "&");
		sb.append("hzip=" + URLEncoder.encode(txnZip, "UTF-8") + "&");
		lnkInfo = sb.toString();

		linkBean.setLnkInfo(lnkInfo);
		linkBean.setLnkState("1");
		linkBean.setLnkDate(new Date());
		linkBean.setLnkRemark(lnkRemark);
		linkBean = linkService.persist(linkBean);
		return SUCCESS;
	}

	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("lnkNo", StringUtil.split(lnkNo));
		paramMap.put("lnkName", lnkName);
		paramMap.put("lnkInfo", lnkInfo);
		paramMap.put("lnkState", lnkState);
		paramMap.put("lnkDate", ParameterUtil.dateCombin(lnkDateTop, lnkDateEnd));
		paramMap.put("lnkRemark", lnkRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "lnkDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TLink> resultSet = linkService.search(querySet);
		linkList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public ILinkService getLinkService() {
		return linkService;
	}

	public void setLinkService(ILinkService linkService) {
		this.linkService = linkService;
	}

	public TLink getLinkBean() {
		return linkBean;
	}

	public void setLinkBean(TLink linkBean) {
		this.linkBean = linkBean;
	}

	public List<TLink> getLinkList() {
		return linkList;
	}

	public void setLinkList(List<TLink> linkList) {
		this.linkList = linkList;
	}

	public String getLnkNo() {
		return lnkNo;
	}

	public void setLnkNo(String lnkNo) {
		this.lnkNo = lnkNo;
	}

	public String getLnkNos() {
		return lnkNos;
	}

	public void setLnkNos(String lnkNos) {
		this.lnkNos = lnkNos;
	}

	public String getLnkName() {
		return lnkName;
	}

	public void setLnkName(String lnkName) {
		this.lnkName = lnkName;
	}

	public String getLnkInfo() {
		return lnkInfo;
	}

	public void setLnkInfo(String lnkInfo) {
		this.lnkInfo = lnkInfo;
	}

	public String getLnkState() {
		return lnkState;
	}

	public void setLnkState(String lnkState) {
		this.lnkState = lnkState;
	}

	public Date getLnkDate() {
		return lnkDate;
	}

	public void setLnkDate(Date lnkDate) {
		this.lnkDate = lnkDate;
	}

	public Date getLnkDateTop() {
		return lnkDateTop;
	}

	public void setLnkDateTop(Date lnkDateTop) {
		this.lnkDateTop = lnkDateTop;
	}

	public Date getLnkDateEnd() {
		return lnkDateEnd;
	}

	public void setLnkDateEnd(Date lnkDateEnd) {
		this.lnkDateEnd = lnkDateEnd;
	}

	public String getLnkRemark() {
		return lnkRemark;
	}

	public void setLnkRemark(String lnkRemark) {
		this.lnkRemark = lnkRemark;
	}

	public IWebsiteService getWebsiteService() {
		return websiteService;
	}

	public void setWebsiteService(IWebsiteService websiteService) {
		this.websiteService = websiteService;
	}

	public String getTxnOrder() {
		return txnOrder;
	}

	public void setTxnOrder(String txnOrder) {
		this.txnOrder = txnOrder;
	}

	public String getTxnCur() {
		return txnCur;
	}

	public void setTxnCur(String txnCur) {
		this.txnCur = txnCur;
	}

	public String getTxnLoc() {
		return txnLoc;
	}

	public void setTxnLoc(String txnLoc) {
		this.txnLoc = txnLoc;
	}

	public String getTxnProduct() {
		return txnProduct;
	}

	public void setTxnProduct(String txnProduct) {
		this.txnProduct = txnProduct;
	}

	public String getTxnFname() {
		return txnFname;
	}

	public void setTxnFname(String txnFname) {
		this.txnFname = txnFname;
	}

	public String getTxnLname() {
		return txnLname;
	}

	public void setTxnLname(String txnLname) {
		this.txnLname = txnLname;
	}

	public String getTxnPhone() {
		return txnPhone;
	}

	public void setTxnPhone(String txnPhone) {
		this.txnPhone = txnPhone;
	}

	public String getTxnEmail() {
		return txnEmail;
	}

	public void setTxnEmail(String txnEmail) {
		this.txnEmail = txnEmail;
	}

	public String getTxnCountry() {
		return txnCountry;
	}

	public void setTxnCountry(String txnCountry) {
		this.txnCountry = txnCountry;
	}

	public String getTxnState() {
		return txnState;
	}

	public void setTxnState(String txnState) {
		this.txnState = txnState;
	}

	public String getTxnCity() {
		return txnCity;
	}

	public void setTxnCity(String txnCity) {
		this.txnCity = txnCity;
	}

	public String getTxnAddr() {
		return txnAddr;
	}

	public void setTxnAddr(String txnAddr) {
		this.txnAddr = txnAddr;
	}

	public String getTxnZip() {
		return txnZip;
	}

	public void setTxnZip(String txnZip) {
		this.txnZip = txnZip;
	}

	public TWebsite getWebsiteBean() {
		return websiteBean;
	}

	public void setWebsiteBean(TWebsite websiteBean) {
		this.websiteBean = websiteBean;
	}

	public List<TWebsite> getWebsiteList() {
		return websiteList;
	}

	public void setWebsiteList(List<TWebsite> websiteList) {
		this.websiteList = websiteList;
	}

	public String getMerNo() {
		return merNo;
	}

	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}

	public String getWebNo() {
		return webNo;
	}

	public void setWebNo(String webNo) {
		this.webNo = webNo;
	}

	public String getWebNos() {
		return webNos;
	}

	public void setWebNos(String webNos) {
		this.webNos = webNos;
	}

	public String getWebSign() {
		return webSign;
	}

	public void setWebSign(String webSign) {
		this.webSign = webSign;
	}

	public String getWebAddress() {
		return webAddress;
	}

	public void setWebAddress(String webAddress) {
		this.webAddress = webAddress;
	}

	public String getWebDescript() {
		return webDescript;
	}

	public void setWebDescript(String webDescript) {
		this.webDescript = webDescript;
	}

	public String getWebOwner() {
		return webOwner;
	}

	public void setWebOwner(String webOwner) {
		this.webOwner = webOwner;
	}

	public String getWebRegister() {
		return webRegister;
	}

	public void setWebRegister(String webRegister) {
		this.webRegister = webRegister;
	}

	public Double getWebValidity() {
		return webValidity;
	}

	public void setWebValidity(Double webValidity) {
		this.webValidity = webValidity;
	}

	public String getWebState() {
		return webState;
	}

	public void setWebState(String webState) {
		this.webState = webState;
	}

	public Date getWebDate() {
		return webDate;
	}

	public void setWebDate(Date webDate) {
		this.webDate = webDate;
	}

	public Date getWebDateTop() {
		return webDateTop;
	}

	public void setWebDateTop(Date webDateTop) {
		this.webDateTop = webDateTop;
	}

	public Date getWebDateEnd() {
		return webDateEnd;
	}

	public void setWebDateEnd(Date webDateEnd) {
		this.webDateEnd = webDateEnd;
	}

	public String getWebVstate() {
		return webVstate;
	}

	public void setWebVstate(String webVstate) {
		this.webVstate = webVstate;
	}

	public Date getWebVdate() {
		return webVdate;
	}

	public void setWebVdate(Date webVdate) {
		this.webVdate = webVdate;
	}

	public Date getWebVdateTop() {
		return webVdateTop;
	}

	public void setWebVdateTop(Date webVdateTop) {
		this.webVdateTop = webVdateTop;
	}

	public Date getWebVdateEnd() {
		return webVdateEnd;
	}

	public void setWebVdateEnd(Date webVdateEnd) {
		this.webVdateEnd = webVdateEnd;
	}

	public String getWebRemark() {
		return webRemark;
	}

	public void setWebRemark(String webRemark) {
		this.webRemark = webRemark;
	}

	public ISubjectService getSubjectService() {
		return subjectService;
	}

	public void setSubjectService(ISubjectService subjectService) {
		this.subjectService = subjectService;
	}

}
